<?php
/**
 * Created by PhpStorm.
 * User: czm
 * Date: 2019-07-25
 * Time: 14:59
 */
namespace app\index\controller;

use think\Controller;
use think\Db;

class Address extends Controller
{
    public function addressList()
    {
        $id = input('id');
        $list = Db::name('address')->where('uid',$id)->find();
        if ($list){
            $da = ['code' => 439, 'msg' => '添加新地址'];
            return json_encode($da,TRUE);
        }

        if(Db::name('address')->where('uid',$id)->select()){
            $da = ['code' => 200, 'msg' => 'OK'];
            return json_encode($da);
        }else{
            $da = ['code' => 403, 'msg' => '失败'];
            return json_encode($da,TRUE);
        }
    }

    public function addressAdd()
    {
        if (request()->isPost()){
            $da = input('post.');
            $id = $da['id'];
            $a = [
                'uid'   =>  $id,
                'name' => input('name'),

                'address' => input('address'),
                'sex'   =>  input('sex'),
                'phone' =>  input('phone'),
            ];
            $r = Db::name('address')->insert($da);
            if ($r){
                $da = ['code' => 200, 'msg' => 'OK'];
                return json_encode($da);
            }else{
                $da = ['code' => 403, 'msg' => '失败'];
                return json_encode($da);
            }
        }
    }

    public function addressEdit()
    {
        $da = input('post.');
        $id = $da['id'];
        $re = Db::name('address')->where('uid',$id)->find();
        if ($re){
            $da = ['code' => 200, 'msg' => '返回修改地址信息'];
            return json_encode($da,TRUE);
        }
        if (request()->isPost()){
            $dat = input('post.');
            $a = [
                'uid'   =>  $id,
                'name' => input('name'),

                'address' => input('address'),
                'sex'   =>  input('sex'),
                'phone' =>  input('phone'),
            ];
            $r = Db::name('address')->where('uid',$id)->update($dat);
            if ($r){
                $da = ['code' => 200, 'msg' => 'OK'];
                return json_encode($da);
            }else{
                $da = ['code' => 403, 'msg' => '失败'];
                return json_encode($da);
            }
        }
    }

    public function addressDel()
    {
        $id = input('id');
        $r = Db::name('address')->where('id',$id)->delete($id);
        if ($r){
            $da = ['code' => 200, 'msg' => '删除成功'];
            return json_encode($da);
        }else{
             $da = ['code' => 403, 'msg' => '失败'];
             return json_encode($da,TRUE);

        }
    }
}